from extract_controller_point import get_model_all_measure_value
import xlwt, xlrd
import os  
  
# 提取指定文件夹中后缀为obj的文件
def get_all_model_filename(file_dir):   
  L=[]   
  for root, dirs, files in os.walk(file_dir):  
    for file in files:  
      if os.path.splitext(file)[1] == '.obj':  
          L.append(os.path.join(root, file))  
  return L

# 批量将文件夹下的obj模型文件的特征尺寸提取并记录到excel表格
def batch_write_excel_xls(xls_path, sheet_name, dir_name):
  files_list = get_all_model_filename(dir_name)
  index = len(files_list)  # 获取需要写入数据的行数

  workbook = xlwt.Workbook()  # 新建一个工作簿
  sheet = workbook.add_sheet(sheet_name)  # 在工作簿中新建一个表格
  # 写列特征名称
  row_name = ['filename', 'height', 'shoulder_width', 'arm_length', 'leg_length', 'chest_length', 'chest_width', 'chest_thickness', 'waist_length', 'waist_width', 'waist_thickness', 'hips_length', 'hips_width', 'hips_thickness', 'right-leg_length', 'right-leg_width', 'right-leg_thickness']
  for i in range(len(row_name)):
    sheet.write(0, i, row_name[i])  # 像表格中写入数据（对应的行和列）

  # 按列写入相应的特征数据
  for i in range(index):
    if i % 10 == 0:
      print(str(i) + '/' + str(index))
    measure_data = get_model_all_measure_value(files_list[i])
    for j in range(len(row_name)):
      sheet.write(i + 1, j, measure_data[row_name[j]])  # 像表格中写入数据（对应的行和列）
  workbook.save(xls_path)  # 保存工作簿
  print("批量测量数据写入数据成功！")


def read_excel(xls_path, sheet_name):
  table_data = xlrd.open_workbook(xls_path).sheet_by_name(sheet_name)
  data_list = []
  row_num = table_data.nrows - 1
  col_num = table_data.ncols
  for i in range(row_num):
    row_data = []
    for j in range(col_num):
      row_data.append(table_data.cell_value(i + 1,j))
    data_list.append(row_data)
  return data_list


if __name__=='__main__':
  # batch_write_excel_xls('D:/桌面/body template/program/excel_data/male.xls', 'measure_data', 'D:\\开发\\Python工程\\measure\\static\\data_file\\humanModel\\male')
  print(read_excel('D:/桌面/body template/program/excel_data/male.xls', 'measure_data')[0])